from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium import webdriver
import time

class Base():
    # 初始化
    def __init__(self,driver):
        self.driver=driver
    # 封装网址
    def open_url(self,url):
        self.driver.get(url)
    # 封装截图
    def error_png(self):
        error_png=time.strftime('%Y%m%d-%H%M%S')
        return error_png
    # 封装定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.driver.command_executor('./'+self.error_png()+'.png')
            print(e)
    # 封装输入事件
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 封装点击事件
    def click(self,loc):
        self.find_element(loc).click()
    # 封装iframe框架
    def iframe(self,loc):
        self.driver.switch_to.frame(self.driver.find_element(*loc))
    # 退出框架
    def tc(self):
        self.driver.switch_to.default_content()
    # 切换窗口
    def window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 提取文字
    def text(self,loc):
        return self.find_element(loc).text

